package com.um.jdy.platform.user.controller;

import com.um.jdy.common.platform.entity.vo.CompanyUserVO;
import com.um.jdy.common.platform.entity.vo.CompanyVO;
import com.um.jdy.platform.user.entity.dto.EditPasswordDTO;
import com.um.jdy.platform.user.entity.dto.LoginDTO;
import com.um.jdy.platform.user.entity.dto.UserEditDTO;
import com.um.jdy.platform.user.entity.vo.PackageVO;
import com.um.jdy.platform.user.manager.UserManager;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 我的-用户
 *
 * @author cs
 * 日期 2023/5/23
 * 版权所有 广州优匠科技有限公司
 */
@RestController
@RequestMapping("/api/v1/user")
@Api(tags = "用户")
@Validated
public class UserController {

    @Autowired
    UserManager userManager;

    @ApiOperation(value = "登录")
    @PostMapping("/signin")
    public Response<String> signin(@RequestBody LoginDTO loginDTO) {
        return userManager.signin(loginDTO.getMobile(), loginDTO.getPassword());
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/signout")
    @TokenRequired
    public Response signout() {
        return userManager.signout();
    }

    @ApiOperation(value = "查询个人资料")
    @GetMapping("/get")
    @TokenRequired
    public Response<CompanyUserVO> get() {
        return Response.success(userManager.get());
    }


    @ApiOperation(value = "读取用户菜单")
    @GetMapping("/menus")
    @TokenRequired
    public Response getUserMenu(){
        return userManager.getUserMenu();
    }


    /**
     * 用户修改密码
     *
     * @param dto
     * @return
     */
    @PutMapping("/edit_password")
    @TokenRequired
    public Response editPassword(@RequestBody EditPasswordDTO dto) {
        boolean result = userManager.editPassword(dto.getOldPassword(),dto.getNewPassword());
        return result ? Response.success() : Response.failed();
    }

    @ApiOperation(value = "修改个人资料")
    @PutMapping("/edit")
    @TokenRequired
    public Response<CompanyUserVO> edit(@RequestBody UserEditDTO dto) {
        return Response.success(userManager.edit(dto));
    }

    @ApiOperation(value = "查询平台使用套餐")
    @GetMapping("/app/package")
    @TokenRequired
    public Response<PackageVO> getAppPackage() {
        return userManager.getAppPackage();
    }
}
